//package com.kt.delivery.utils;
//
//import com.fasterxml.jackson.databind.JsonNode;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.kt.delivery.config.WxPayConfig;
//import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
//import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
//import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
//import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
//import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
//import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.io.ByteArrayInputStream;
//import java.io.IOException;
//import java.io.UnsupportedEncodingException;
//import java.security.*;
//import java.util.Base64;
//
///**
// * @ClassName WechatPayUtils
// * @Description 签名工具类
// * @Author ghs
// * @Date 2024-04-02 23:34
// * @Version 1.0
// */
//@Component
//public class WechatPayUtils {
//
//    @Autowired
//    private WxPayConfig wxPayConfig;
//
//
//
//    /**
//     * @Description: 获取httpClient
//     * @Param: []
//     * @return: org.apache.http.impl.client.CloseableHttpClient
//     * @Author: XQD
//     * @Date:2022/8/13 23:16
//     */
////    public  CloseableHttpClient getHttpClient() {
////        CloseableHttpClient httpClient = null;
////
////        System.out.println(wxPayConfig.getPrivateKey());
////        // 加载商户私钥（privateKey：私钥字符串）
////        try {
////            PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(
////                    new ByteArrayInputStream(wxPayConfig.getPrivateKey().getBytes("utf-8")));
////            //使用自动更新的签名验证器，不需要传入证书
////            AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
////                    new WechatPay2Credentials(wxPayConfig.getMchId(), new PrivateKeySigner(wxPayConfig.getMCH_SERIAL_NO(), merchantPrivateKey)),
////                    wxPayConfig.getAPI_V3KEY().getBytes("utf-8"));
////            // 初始化httpClient
////            httpClient = WechatPayHttpClientBuilder.create()
////                    .withMerchant(wxPayConfig.getMchId(), wxPayConfig.getMCH_SERIAL_NO(), merchantPrivateKey)
////                    .withValidator(new WechatPay2Validator(verifier))
////                    .build();
////        } catch (UnsupportedEncodingException e) {
////            e.printStackTrace();
////        }
////        return httpClient;
////    }
//
//
//    /**
//     * @Description: 生成签名
//     * @Param: [message]
//     * @return: java.lang.String
//     * @Author: XQD
//     * @Date:2022/6/7 16:00
//     */
//    public  String sign(byte[] message) throws Exception {
//        Signature sign = Signature.getInstance("SHA256withRSA");
//        PrivateKey privateKey = PemUtil.loadPrivateKey(new ByteArrayInputStream(wxPayConfig.getPrivateKey().getBytes("utf-8")));
//        sign.initSign(privateKey);
//        sign.update(message);
//        return Base64.getEncoder().encodeToString(sign.sign());
//    }
//
//
//    /**
//     * @Description: 验证签名
//     * @Param: [serial, message, signature]请求头中带的序列号, 验签名串, 请求头中的应答签名
//     * @return: boolean
//     * @Author: XQD
//     * @Date:2021/9/14 10:36
//     */
//    public  boolean signVerify(String serial, String message, String signature) {
//        AutoUpdateCertificatesVerifier verifier = null;
//        try {
//            // 加载商户私钥（privateKey：私钥字符串）
//            PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(
//                    new ByteArrayInputStream(wxPayConfig.getPrivateKey().getBytes("utf-8")));
//
//            //使用自动更新的签名验证器，不需要传入证书
//            verifier = new AutoUpdateCertificatesVerifier(
//                    new WechatPay2Credentials(wxPayConfig.getMchId(), new PrivateKeySigner(wxPayConfig.getKeyPath(), merchantPrivateKey)),
//                    wxPayConfig.getAPI_V3KEY().getBytes("utf-8"));
//            return verifier.verify(serial, message.getBytes("utf-8"), signature);
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
//
//    /**
//     * @Description: 解密订单信息
//     * @Param: [body] 应答报文主体
//     * @return: java.lang.String
//     * @Author: XQD
//     * @Date:2021/9/14 11:48
//     */
//    public  String decryptOrder(String body) {
//        try {
//            AesUtil aesUtil = new AesUtil(wxPayConfig.getAPI_V3KEY().getBytes("utf-8"));
//            ObjectMapper objectMapper = new ObjectMapper();
//            JsonNode node = objectMapper.readTree(body);
//            JsonNode resource = node.get("resource");
//            String ciphertext = resource.get("ciphertext").textValue();
//            String associatedData = resource.get("associated_data").textValue();
//            String nonce = resource.get("nonce").textValue();
//            return aesUtil.decryptToString(associatedData.getBytes("utf-8"), nonce.getBytes("utf-8"), ciphertext);
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (GeneralSecurityException e) {
//            e.printStackTrace();
//        }
//        return "";
//    }
//}
